package vn.com.ltv.lo.daos;

import java.util.ArrayList;

import vn.com.ltv.lo.dtos.ImageDTO;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class ImageDAO {
	protected static final String TABLE_NAME = "Image";
	protected static final String ID = "rowid";
	protected static final String IMG_ID = "img_id";
	protected static final String IMG_PTH = "img_pth";
	protected static final String IMG_NM = "img_nm";
	protected static String TABLE_CREATION_STRING = "create table "
			+ TABLE_NAME + " (" + ID + " integer primary key autoincrement, "
			+ IMG_ID + " text, " + IMG_PTH + " text, " + IMG_NM + " text);";
	protected static String TABLE_DROP_STRING = "drop table if exists"
			+ TABLE_NAME + ";";
	private Context context;

	public ImageDAO(Context context) {
		this.context = context;
	}

	public ArrayList<ImageDTO> getAll() {
		ArrayList<ImageDTO> imageDTOs = new ArrayList<ImageDTO>();
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getReadableDatabase();
		Cursor cursor = sQLiteDatabase.query(TABLE_NAME, null, null, null,
				null, null, null);
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				ImageDTO imageDTO = this.cursorToImageDTO(cursor);
				imageDTOs.add(imageDTO);
				cursor.moveToNext();
			}
		}
		cursor.close();
		sQLiteDatabase.close();
		databaseHelper.close();
		return imageDTOs;
	}
	
	public String getPathImage(int idImage){
		String path = "";
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getReadableDatabase();
		Cursor cursor = sQLiteDatabase.query(TABLE_NAME, null, IMG_ID + "=?",
				new String[] { String.valueOf(idImage) }, null, null, null);
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				path = cursor.getString(cursor.getColumnIndex(IMG_PTH));
				cursor.moveToNext();
			}
		}
		cursor.close();
		sQLiteDatabase.close();
		databaseHelper.close();
		
		return path;
	}
	
	public String getPathImage(String idImage){
		String path = "";
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getReadableDatabase();
		Cursor cursor = sQLiteDatabase.query(TABLE_NAME, null, IMG_ID + "=?",
				new String[] { String.valueOf(idImage) }, null, null, null);
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				path = cursor.getString(cursor.getColumnIndex(IMG_PTH));
				cursor.moveToNext();
			}
		}
		cursor.close();
		sQLiteDatabase.close();
		databaseHelper.close();
		
		return path;
	}

	public ImageDTO get(int id) {
		ImageDTO imageDTO = null;
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getReadableDatabase();
		Cursor cursor = sQLiteDatabase.query(TABLE_NAME, null, ID + "=?",
				new String[] { String.valueOf(id) }, null, null, null);
		if (cursor.moveToFirst()) {
			imageDTO = this.cursorToImageDTO(cursor);
		}
		cursor.close();
		sQLiteDatabase.close();
		databaseHelper.close();
		return imageDTO;
	}

	public void insert(ImageDTO imageDTO) {
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getWritableDatabase();
		ContentValues contentValues = this.imageDTOToContentValues(imageDTO);
		sQLiteDatabase.insert(TABLE_NAME, null, contentValues);
		sQLiteDatabase.close();
		databaseHelper.close();
	}

	/** * @return the number of rows affected */
	public int update(ImageDTO imageDTO) {
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getWritableDatabase();
		int id = imageDTO.getId();
		imageDTO.setId(-1);
		ContentValues contentValues = this.imageDTOToContentValues(imageDTO);
		int rowCount = sQLiteDatabase.update(TABLE_NAME, contentValues, ID
				+ "=?", new String[] { String.valueOf(id) });
		sQLiteDatabase.close();
		databaseHelper.close();
		return rowCount;
	}

	public void delete(int id) {
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getWritableDatabase();
		sQLiteDatabase.delete(TABLE_NAME, ID + "=?",
				new String[] { String.valueOf(id) });
		sQLiteDatabase.close();
		databaseHelper.close();
	}

	private ImageDTO cursorToImageDTO(Cursor cursor) {
		ImageDTO imageDTO = new ImageDTO();
		int id = cursor.getInt(cursor.getColumnIndex(ID));
		imageDTO.setId(id);
		String imgId = cursor.getString(cursor.getColumnIndex(IMG_ID));
		imageDTO.setImgId(imgId);
		String imgPth = cursor.getString(cursor.getColumnIndex(IMG_PTH));
		imageDTO.setImgPth(imgPth);
		String imgNm = cursor.getString(cursor.getColumnIndex(IMG_NM));
		imageDTO.setImgNm(imgNm);
		return imageDTO;
	}

	private ContentValues imageDTOToContentValues(ImageDTO imageDTO) {
		ContentValues contentValues = new ContentValues();
		int id = imageDTO.getId();
		if (id >= 0) {
			contentValues.put(ID, id);
		}
		String imgId = imageDTO.getImgId();
		if (imgId != null) {
			contentValues.put(IMG_ID, imgId);
		}
		String imgPth = imageDTO.getImgPth();
		if (imgPth != null) {
			contentValues.put(IMG_PTH, imgPth);
		}
		String imgNm = imageDTO.getImgNm();
		if (imgNm != null) {
			contentValues.put(IMG_NM, imgNm);
		}
		return contentValues;
	}
}